import {
    approval,
    createOdCharge,
    deleteOdChargeById,
    getOdChargeById,
    queryChargesForPage,
    totalMoney,
    updateOdCharge
} from '../../services/logisticsOrder/orderCharge';

import {getOrderList, getById, deleteById, updateOrder} from '../../services/logisticsOrder/trailer'


import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'trailer',
    state: {
        page: {},
        list: [],
        dataSource: {},
        oneDetail: {}
    },

    effects: {

        //获取列表
        * getOrderList({payload}, {call, put}) {

            console.log("  * getOrderList({payload}, {call, put})",payload)
            const res = yield call(getOrderList, payload);
            // console.log(res)
            if (res && res.data.resultCode === "ok") {
                // message.info("费用添加成功！");
                yield put({
                    type: "refreshState",
                    payload: res.data.content
                })
            } else {
            }
        },


        // 更新费用
        * updateOrder({payload}, {call, put}) {
            console.log("* updateOrder({payload}, {call, put}) {",payload)
            const res = yield call(updateOrder, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },


        // 根据id删除订单费用
        * deleteById({payload, callback}, {call}) {
            const res = yield call(deleteById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                if (payload.onSuccess) payload.onSuccess();
                if (callback) callback();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },


        // 根据id获取费用
        * getById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getById, {...R.omit(['onSuccess', 'onFail'])(payload)});

                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'saveOneDetail',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'saveOneDetail',
                    payload: {
                        dataSource: {}

                    }
                });
            }
        },


    },


    reducers: {
        refreshState(state, {payload}) {
            return {
                ...state,
                ...payload
            }

        },

        saveOneDetail(state, {payload}) {
            return {
                ...state,
                oneDetail:payload}
        },

    },


    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if (menus && !menus.find(item => item.includes(pathname)) && pathname != '/logout') {
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }


};